<template>
  <div class="container">
    <div class="container-title">
      <p>失物招领</p>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item>社区服务</el-breadcrumb-item>
        <el-breadcrumb-item>失物招领</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <div class="search-box">
      <el-form ref="form" :model="searchData">
        <el-form-item>
          <el-select v-model="searchData.status" filterable placeholder="状态">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <div class="block">
          <el-date-picker
            v-model="searchData.date"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </div>
        <el-form-item>
          <el-button
            type="success"
            @click="onSearch"
            icon="el-icon-search"
            style="background-color: #0bb197"
            >查询</el-button
          ><el-button @click="onReset" icon="el-icon-refresh" class="reset"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <!-- 表格 -->
    <div class="data-list">
      <div class="data-list-title">
        <span>查询结果列表</span>
        <el-button
          type="success"
          style="background-color: #0bb197"
          @click="publish"
          >发布招领</el-button
        >
      </div>
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column prop="lfId" label="序号" align="center">
        </el-table-column>
        <el-table-column prop="publisher" label="发布人" align="center">
        </el-table-column>
        <el-table-column
          prop="contents"
          label="招领内容"
          width="250"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="releaseTime"
          label="发布时间"
          width="160"
          align="center"
        >
          <template slot-scope="scope">
            {{ timeTrans(scope.row.releaseTime, 0) }}</template
          >
        </el-table-column>
        <el-table-column prop="contacts" label="联系人" align="center">
        </el-table-column>
        <el-table-column prop="contactNumber" label="联系电话" align="center">
        </el-table-column>
        <el-table-column prop="contactAddress" label="联系地址" align="center">
        </el-table-column>
        <el-table-column prop="completionTime" label="完成时间" align="center">
          <template slot-scope="scope">
            {{
              timeTrans(scope.row.completionTime, 1) == '1970-01-01'
                ? ''
                : timeTrans(scope.row.completionTime, 1)
            }}</template
          >
        </el-table-column>
        <el-table-column prop="state" label="状态" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.state == 0 ? '发布中' : '已完成' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button @click="detail(scope.row)" type="text" size="small"
              >详情</el-button
            >
            <el-button
              v-if="scope.row.state == 0"
              @click="finishOpen(scope.row)"
              type="text"
              size="small"
              >完成</el-button
            >
          </template></el-table-column
        >
      </el-table>
      <!-- 页码 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :background="true"
      >
      </el-pagination>
    </div>
    <!-- 发布招领弹窗 -->
    <el-dialog
      title="发布招领"
      :visible.sync="publishDialog"
      width="820px"
      :destroy-on-close="true"
      class="add"
    >
      <el-form :model="publishData">
        <el-form-item label="招领内容">
          <el-input
            type="textarea"
            :rows="3"
            placeholder="请输入内容"
            v-model="publishData.contents"
          >
          </el-input>
        </el-form-item>
        <div class="contact">
          <el-form-item label="联系人">
            <el-input
              v-model="publishData.contacts"
              autocomplete="off"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
          <el-form-item label="联系电话">
            <el-input
              v-model="publishData.contactNumber"
              autocomplete="off"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
        </div>
        <div class="address">
          <el-form-item label="联系地址">
            <el-input
              v-model="publishData.contactAddress"
              autocomplete="off"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="publishDialog = false">取 消</el-button>
        <el-button type="primary" @click="save">保 存</el-button>
      </div>
    </el-dialog>
    <!-- 详情信息弹窗 -->
    <el-dialog
      title="详细信息"
      :visible.sync="detailDialog"
      width="43%"
      class="detail"
    >
      <div class="detail-content">
        <p class="found-content">招领内容：{{ detailData[0].contents }}</p>
        <p class="contact">联系人：{{ detailData[0].contacts }}</p>
      </div>
      <div class="detail-information">
        <p class="tel">联系电话：{{ detailData[0].contactNumber }}</p>
        <p class="address">联系地址：{{ detailData[0].contactAddress }}</p>
      </div>
      <div class="detail-table">
        <div class="table-title">招领记录：</div>
        <el-table :data="detailData">
          <el-table-column
            property="lfId"
            label="序号"
            align="center"
          ></el-table-column>
          <el-table-column property="state" label="操作" align="center">
            <template slot-scope="scope">
              <span>{{ scope.state == 0 ? '招领完成' : '发布招领' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            property="publisher"
            label="操作人"
            align="center"
          ></el-table-column>
          <el-table-column
            property="releaseTime"
            label="操作时间"
            align="center"
          >
          </el-table-column>
        </el-table>
      </div>
    </el-dialog>
    <!-- 失物招领完成弹窗 -->
    <el-dialog :visible.sync="finishDialog" width="393px" center class="finish">
      <span>请问是否确认失物招领已完成？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="finishDialog = false">取 消</el-button>
        <el-button type="primary" @click="finish">保 存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import time from '../utils/time';

export default {
  mixins: [time],
  data: function () {
    return {
      pageNum: 1,
      pageSize: 5,
      total: 0,
      currentPage: 1,
      //搜索
      options: [
        {
          value: 0,
          label: '发布中',
        },
        {
          value: 1,
          label: '已完成',
        },
      ],
      searchData: { status: '', date: [] },
      // 数据渲染
      tableData: [],
      // 发布招领弹窗
      publishDialog: false,
      publishData: {
        completionTime: '',
        contactAddress: '',
        contactNumber: '',
        contacts: '',
        contents: '',
        publisher: '',
        releaseTime: '',
        state: 0,
      },

      // 详情弹窗
      detailDialog: false,
      detailData: [
        {
          lfId: 0,
          publisher: '',
          contents: '',
          releaseTime: 0,
          completionTime: 0,
          contacts: '',
          contactNumber: '',
          contactAddress: '',
          state: 0,
        },
      ],
      //失物招领完成弹窗
      finishDialog: false,
      finishData: {},
    };
  },
  methods: {
    //渲染
    getData: function () {
      this.$axios({
        url:
          '/smart/lost-found/list' + '/' + this.pageNum + '/' + this.pageSize,
      }).then(({ data }) => {
        if (data.code == '00000') {
          this.tableData = data.data.pageInfo.list;
          this.total = data.data.pageInfo.total;
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    //搜索
    onSearch: function () {
      this.$axios({
        url: '/smart/lost-found/search',
        method: 'post',
        params: { pageNum: 1, pageSize: this.pageSize },
        data: {
          startTime: this.searchData.date[0],
          endTime: this.searchData.date[1],
          state: Number(this.searchData.status),
        },
      }).then(({ data }) => {
        if (data.code == '00000') {
          if (data.data.pageInfo.list.length > 0) {
            this.tableData = data.data.pageInfo.list;
            this.$message.success(data.msg);
          } else {
            this.tableData = data.data.pageInfo.list;
            this.$message.error('暂无数据！');
          }
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    onReset: function () {
      this.searchData = { status: '', date: [] };
      this.getData();
    },
    //详情
    detail: function (row) {
      this.detailDialog = true;
      this.$axios({
        url: '/smart/lost-found/queryById/' + row.lfId,
      }).then(({ data }) => {
        if (data.code == '00000') {
          this.detailData = [data.data];
          this.detailData[0].releaseTime = this.timeTrans(
            this.detailData[0].releaseTime,
            0
          );
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    finishOpen: function (row) {
      this.finishDialog = true;
      this.finishData = row;
    },
    finish: function () {
      this.finishData.state = 1;
      this.finishData.completionTime = Date.now();
      this.$axios({
        url: '/smart/lost-found/edit',
        method: 'put',
        data: this.finishData,
      }).then(({ data }) => {
        if (data.code == '00000') {
          this.finishDialog = false;
          this.$message.success(data.msg);
          this.getData();
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.getData();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum = val;
      this.getData();
    },
    //发布招领
    publish: function () {
      this.publishDialog = true;
    },
    //发布保存
    save: function () {
      this.publishData.publisher = JSON.parse(
        sessionStorage.getItem('user')
      ).username;
      this.publishData.releaseTime = Date.now();
      this.$axios({
        url: '/smart/lost-found/add',
        method: 'post',
        data: this.publishData,
      }).then(({ data }) => {
        if (data.code == '00000') {
          this.publishDialog = false;
          this.publishData = {};
          this.$message.success(data.msg);
          this.getData();
        } else {
          this.$message.error(data.msg);
        }
      });
    },
  },
  mounted: function () {
    this.getData();
  },
};
</script>

<style lang="less" scoped>
.container {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  .container-title {
    width: 98%;
    height: 80px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    p {
      color: #333333;
      font-size: 16px;
      font-weight: 400;
      font-family: 'Arial Normal', 'Arial';
    }
    .el-breadcrumb-item {
      color: rgba(148, 148, 148, 0.898039215686275);
      font-size: 12px;
      font-weight: 400;
      font-family: 'Arial Normal', 'Arial';
    }
  }
  .search-box {
    width: 98%;
    height: 80px;
    background: #ffffff;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    border: 1px solid #ffffff;
    border-radius: 5px;
    .el-form {
      width: 98%;
      background: #ffffff;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      .el-form-item {
        margin-left: 10px;
        margin-bottom: 0;
        .el-select {
          width: 155px;
        }
        .el-button {
          margin: 10px;
        }
        .reset {
          background-color: white;
          color: #c0c4cc;
          border: 1px solid #c0c4cc;
        }
        .reset:hover {
          color: #0bb197;
        }
      }
      .block {
        margin-left: 10px;
      }
      .el-date-editor--datetimerange.el-input__inner {
        width: 310px;
      }
    }
  }
  .data-list {
    width: 98%;
    height: 100%;
    background: #ffffff;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-around;
    margin-top: 20px;
    .data-list-title {
      width: 98%;
      height: 60px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      span {
        height: 16px;
        font-family: 'Arial Normal', 'Arial';
        font-weight: 700;
        font-style: normal;
        font-size: 13px;
        color: #3c4145;
      }
    }
    .el-table {
      flex: 1;
      .el-button {
        color: #0bb197;
      }
    }
    .el-pagination {
      width: 98%;
      height: 60px;
      display: flex;
      justify-content: flex-end;
      align-items: center;
    }
  }
}
</style>
<style lang="less">
.el-pagination.is-background .el-pager li:not(.disabled):hover {
  color: #0bb197;
}
.el-pagination.is-background .el-pager li:not(.disabled).active {
  background-color: #0bb197;
  color: #fff;
}
// 发布招领弹窗
.add .el-dialog {
  display: flex;
  flex-direction: column;
  align-items: center;
  .el-dialog__header {
    width: 100%;
    height: 70px;
    padding: 0;
    display: flex;
    justify-content: center;
    .el-dialog__title {
      width: 95%;
      line-height: 60px;
      font-size: 15px;
      font-weight: bold;
      color: #000000;
      text-align: left;
    }
    .el-dialog__headerbtn .el-dialog__close {
      color: #dfe1e8;
    }
  }
  .el-dialog__body {
    width: 95%;
    padding: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    .el-form {
      width: 95%;
      display: flex;
      flex-direction: column;
      align-items: center;
      .el-form-item {
        width: 100%;
        .el-textarea {
          width: 100%;
        }
      }
      .contact {
        width: 100%;
        display: flex;
        justify-content: space-between;
        align-items: center;
        .el-form-item {
          width: 48%;
          .el-input {
            width: 100%;
          }
        }
      }
      .address {
        width: 100%;
        display: flex;
        justify-content: space-between;
        align-items: center;
        .el-form-item {
          width: 48%;
          .el-input {
            width: 100%;
          }
        }
      }
    }
  }
  .el-dialog__footer {
    width: 95%;
    padding: 0;
    .dialog-footer {
      height: 70px;
      display: flex;
      justify-content: space-evenly;
      align-items: center;
      .el-button--primary {
        color: #fff;
        background-color: #0bb197;
        border-color: #0bb197;
      }
      .el-button--default {
        color: #333333;
        background-color: #dfe1e8;
        border-color: #dfe1e8;
      }
      .el-button--primary:focus,
      .el-button--primary:hover {
        color: #ffffff;
        border-color: #0bb197;
        background-color: #0bb197;
      }
    }
  }
}
// 详情信息弹窗
.detail {
  display: flex;
  flex-direction: column;
  align-items: center;
  .el-dialog__header {
    width: 100%;
    height: 47px;
    color: #ffffff;
    background: #0bb197;
    padding: 0;
    .el-dialog__title {
      line-height: 47px;
      font-size: 15px;
      color: #ffffff;
      margin-left: 20px;
    }
    .el-dialog__headerbtn .el-dialog__close {
      color: #ffffff;
    }
  }
  .el-dialog__body {
    width: 95%;
    height: 453px;
    padding: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-evenly;
    .detail-content {
      width: 95%;
      height: 60px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .found-content {
        width: 80%;
        text-align: left;
      }
      .contact {
        width: 15%;
        text-align: left;
      }
    }
    .detail-information {
      width: 95%;
      height: 60px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .tel {
        width: 80%;
        text-align: left;
      }
      .address {
        width: 15%;
        text-align: left;
      }
    }
    .detail-table {
      width: 95%;
      flex: 1;
      .table-title {
        height: 60px;
        line-height: 60px;
      }
    }
  }
}
//失物招领完成弹窗
.finish {
  display: flex;
  flex-direction: column;
  align-items: center;
  .el-dialog__header {
    width: 393px;
    height: 37px;
    color: #ffffff;
    padding: 0;
    .el-dialog__headerbtn .el-dialog__close {
      color: #ffffff;
    }
  }
  .el-dialog__body {
    width: 393px;
    height: 87px;
    padding: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-evenly;
  }
  .el-dialog__footer {
    width: 393px;
    height: 77px;
  }
}
</style>
